<?php
require_once(SYS_CLASSDIR . "controller.interface.php");

class controller {
	protected $controllerChild;
	protected $userObj;
	protected $runtimeConfiguration = array("outputMode" => "HTML");

	public function __construct($controllerName){
		$origController = $controllerName;
		$controllerName = "cntrl" . $controllerName;
		$path = dirname(__FILE__) . "/controllers/" . $controllerName . ".class.php";
		if(file_exists($path)){
			require_once($path);	
			
			ini_set('session.gc_maxlifetime', (60 * 30) ); // 30mins
			//ini_set('session.gc_probability', 0);
			//ini_set('session.gc_divisor', 1); 
			
			
			// Start the session
			session_start();
			
			$tmpUserObj = isset($_SESSION['userObject']) ? unserialize($_SESSION['userObject']) : '';
			
			if(is_object($tmpUserObj) && ($tmpUserObj instanceof user)){
				$this->userObj = $tmpUserObj;
			} else {
				// Create the state class
				$this->userObj = new user();
			}
			
			// Load the controller and set it as the controllerChild.
			$this->controllerChild = new $controllerName($this->userObj);
			
			define('SYS_LOGINSTATE', $this->userObj->getAccessLevel());
			
			// The controller MUST implement viewController to be allowed to interact with the main system.
			if(in_array("viewController", class_implements($this->controllerChild)) == false) throw new PicBoardException("Controller exception: Requested controller '" . $origController . "' does not implement viewController.", 509);
		} else {
			throw new PicBoardException("Controller exception: Requested controller '" . $origController . "' does not exist.", 404);
		}
	}
	
	final public function getSide(){
		return $this->controllerChild->getSide();
	}
	
	final public function getUserNameState(){
		if($this->userObj->getAccessLevel() > -1){
			return "Logged in as <b>" . $this->userObj->getUserName() . "</b>";
		} else {
			return "";
		}
	}
	
	final public function getArea(){
		return $this->controllerChild->getArea();
	}
	
	final public function getDependencies(){
		//	Dependencies
		$dependencies = $this->controllerChild->getDependencies();
		
		$dependencies['CSS'] = isset($dependencies['CSS']) ? $dependencies['CSS'] : array();
		$dependencies['JavaScript'] = isset($dependencies['JavaScript']) ? $dependencies['JavaScript'] : array();
		$dependencies['InlineJavaScript'] = isset($dependencies['InlineJavaScript']) ? $dependencies['InlineJavaScript'] : array();
		
		return $dependencies;
	}
	
	final public function getPageName(){
		return $this->controllerChild->getPageName();
	}
	
	final public function getTools(){
		$logoutLink = (SYS_LOGINSTATE != -1) ? new template("toolset.logout") : '';
		return $this->controllerChild->getTools() . $logoutLink;
	}
	
	final public function getRuntimeConfiguration(){
		if( method_exists($this->controllerChild, 'setRuntimeConfiguration') ){
			return array_merge( $this->runtimeConfiguration, count($this->controllerChild->setRuntimeConfiguration()) > 0 ? $this->controllerChild->setRuntimeConfiguration() : array() );
		} else {
			return $this->runtimeConfiguration;
		}
	}
	

}

?>